package com.zzyk.main.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.zzyk.main.service.IMsgService;
import com.zzyk.main.service.IRedisService;
import com.zzyk.main.utils.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MsgServiceImpl implements IMsgService {
    private static final int EXPIRE = 300 * 1000;
    private static final String REGION_ID = "cn-hangzhou";
    private static final String ACCESS_KEY_ID = "LTAI4GCotqmzEorG1YbADdpR";
    private static final String SECRET = "99VTOOQKGqMdkGZmCrLT978qEeF0R8";
    private static final String SIGN_NAME = "米塔网络";
    private static final String TEMPLATE_CODE = "SMS_197110085";
    private static final String SMS_205465134 = "SMS_205465134";
    private static final String SMS_207501642 = "SMS_207501642";

    @Autowired
    private IRedisService redisService;

    @Override
    public String send(String username) {
        String code = redisService.get(username);
        if (code == null) {
            code = RandomUtils.randomNumberString(6);
        }

        DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        StringBuffer payload = new StringBuffer("{\"code\":\"");
        payload.append(code).append("\"}");

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", REGION_ID);
        request.putQueryParameter("PhoneNumbers", username);
        request.putQueryParameter("SignName", SIGN_NAME);
        request.putQueryParameter("TemplateCode", TEMPLATE_CODE);
        request.putQueryParameter("TemplateParam", payload.toString());
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return redisService.set(username, code, EXPIRE);
    }

    @Override
    public void activationSend(String phone,String sms) {

        DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        StringBuffer payload = new StringBuffer("{\"phone\":\"");
        payload.append(phone).append("\"}");
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", REGION_ID);
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", SIGN_NAME);
        request.putQueryParameter("TemplateCode", sms);
        request.putQueryParameter("TemplateParam", payload.toString());
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String get(String username) {
        String code = redisService.get(username);
        del(username);
        return code;
    }

    @Override
    public void del(String username) {
        redisService.del(username);
    }
}
